//当前的登录页面已经完成异常捕获改写


package auto.common;
import io.appium.java_client.AppiumBy;
import io.appium.java_client.AppiumDriver;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.TimeoutException;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Login {

    public static String login(WebDriverWait wait, AppiumDriver driver, String accountUser, String passwordUser) throws InterruptedException {
        try{
            //如果没有历史账号则会使用外测服务器进入
            Thread.sleep(500);
            if(driver.findElements(AppiumBy.iOSClassChain("**/XCUIElementTypeStaticText[`name == \"进入\"`]")).size()>=1){
                driver.findElement(AppiumBy.iOSClassChain("**/XCUIElementTypeStaticText[`name == \"进入\"`]")).click();
            }else{
                System.out.println("非首次进入服务器");
            }

            //判断历史页面是否存在，历史页面如果存在则需要点击添加账号才能进入输入页面
            if(driver.findElements(AppiumBy.accessibilityId("添加其他账户")).size()>=1){
                driver.findElement(AppiumBy.accessibilityId("添加其他账户")).click();
                //点击添加之后，判断是否达到上限
                if(driver.findElements(AppiumBy.iOSClassChain("**/XCUIElementTypeStaticText[`name == \"账户添加上限\"`]")).size()>=1){
                    System.out.println("历史账号页面已经达到上限，清理后再执行注册");
                    return "历史账号页面已经达到上限，清理后再执行注册";
                }
            }else{
                System.out.println("不存在历史账号页面");
            }


            //在这里判断是邮箱还是手机号，如果含有就不用执行任何操作，如果不包含就要进行切换
            if (accountUser.contains("@")) {
                WebElement textField1 = wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain
                        ("**/XCUIElementTypeTextField[`value == \"请输入你的邮箱\"`]")));
                textField1.click();
                textField1.sendKeys(accountUser);
            } else {
                wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("手机号码登录"))).click();
                //点击输入号码的文本框
                WebElement textField2 = wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain
                        ("**/XCUIElementTypeTextField[`value == \"输入电话号码\"`]")));
                textField2.click();
                textField2.sendKeys(accountUser);
            }

            //输入号码之后点击获取验证码
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain
                    ("**/XCUIElementTypeButton[`name == \"获取验证码\"`][2]"))).click();

            //处理异常情况
            if (driver.findElements(AppiumBy.accessibilityId("该账号已登录")).size() >= 1) {
                System.out.println("该账号已登录");
                return "该账号已登录";
            } else if (driver.findElements(AppiumBy.accessibilityId("电话号码无效")).size() >= 1) {
                System.out.println("输入账号是无效的");
                return "输入账号无效";
            } else if (driver.findElements(AppiumBy.accessibilityId("邮箱格式错误")).size() >= 1) {
                System.out.println("输入邮箱格式错误");
                return "邮箱格式错误";
            }

            //判断账号是否注册的弹窗已经弹出，出现了就点击确认按钮
            WebElement enter=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("确定")));
            enter.click();

            System.out.println("10s内滑块验证手动实现");

            //外测中使用的固定的验证码
            String verify = "306908";
            WebElement verificationCode =wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.className("XCUIElementTypeTextField")));
            verificationCode.sendKeys(verify);


            //输入两步验证密码
            WebElement pinCode = wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain
                    ("**/XCUIElementTypeSecureTextField[`value == \"请输入两步验证密码\"`]")));
            pinCode.click();
            pinCode.sendKeys(passwordUser);


            //等待下一步按钮高亮开始点击
            WebElement next1 = wait.until(ExpectedConditions.visibilityOfElementLocated
                    (AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == \"下一步\"`]")));
            wait.until(ExpectedConditions.attributeToBe(next1, "enabled", "true"));//灰色的是false，变蓝了才能点击
            next1.click();


            //判断两步验证密码提示页面是否出现
            if(driver.findElements(AppiumBy.iOSClassChain("**/XCUIElementTypeTextField[`value == \"密码提示（可选）\"`]")).size()>=1){
                //开始输入两步验证密码的提示
                WebElement passwordTip=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeTextField[`value == \"密码提示（可选）\"`]")));
                passwordTip.sendKeys("123456");

                //点击出现的下一步
                WebElement next3 = wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == '下一步'`]")));
                wait.until(ExpectedConditions.attributeToBe(next3, "enabled", "true"));//灰色的是false，变蓝了才能点击
                next3.click();

                //输入恢复邮箱
                WebElement restoreEmail=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeTextField[`value == \"输入您的邮箱地址\"`]")));
                restoreEmail.sendKeys("123456@gmail.com");

                //点击出现的下一步
                WebElement next4 = wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == '下一步'`]")));
                wait.until(ExpectedConditions.attributeToBe(next4, "enabled", "true"));//灰色的是false，变蓝了才能点击
                next4.click();

                //需要进行滑块验证，10秒内完成
                System.out.println("请在10秒内完成滑块验证");

                //输入恢复邮箱收到的验证码，外测环境依然是306908
                String verify1 = "306908";
                WebElement verificationCode1 =wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.className("XCUIElementTypeTextField")));
                verificationCode1.sendKeys(verify1);
            }else{
                System.out.println("当前登录账号已经设置过提示密码和回复邮箱");
            }

            //查看是否进入聊天列表页面
            try{
                wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("联系人")));
            }catch(TimeoutException e){
                System.out.println("没有成功进入聊天列表页面");
                return "没有成功进入聊天列表页面";
            }

            System.out.println("登录功能正向流程正常");
            return "正常";
        }catch(TimeoutException | NoSuchElementException e1){
            System.out.println("登录流程出现异常，请检查");
            return "登录流程出现异常，请检查";
        }
    }
}
